#include "BlockSet.h"
#include <assert.h>

namespace ATSudokuEngine
{

Bool BlockSet::Value(UInt32 nValue)
{
	assert(pCell_);
	if(Null == pCell_)
		return False;

	UInt32 nOldValue = pCell_->Value(nValue);
	if(False == IsValid())
	{
		pCell_->Value(nOldValue);
		return False;
	}

	return True;
}

Bool BlockSet::IsValid()
{
	if(False == Blocks_[0].IsValid())
		return False;
	if(False == Blocks_[1].IsValid())
		return False;
	if(False == Blocks_[2].IsValid())
		return False;

	return True;
}

void BlockSet::SetBlocks(const Cell* pCell, const Block& aRowBlock, const Block& aColBlock, const Block& aSquareBlock)
{
	assert(pCell);

	pCell_ = pCell;
	Blocks_[0] = aRowBlock;
	Blocks_[1] = aColBlock;
	Blocks_[2] = aSquareBlock;
}

void BlockSet::UpdateCandidates() const
{
	UInt32 nSize = Blocks_[0].Size();
	
	Candidate aCandidate(nSize);
	
	for(UInt32 i=0; i<3; ++i)
	{
		for(UInt32 j=0; j<nSize; ++j)
		{
			const Cell* pCell = Blocks_[i].At(j);
			assert(pCell);

			if(INVALID_VALUE != pCell->Value())
				aCandidate.Remove(pCell->Value());
		}
	}

	assert(pCell_);
	if(pCell_)
		pCell_->SetCandidate(aCandidate);

}

}	// namespace ATSudokuEngine
